import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/view/common/Index'
import ContentMain from '@/components/view/common/ContentMain'
import Login from '@/components/view/login/Login'
import ConfigRouter from '@/components/view/config/index'
import ProjectRouter from '@/components/view/project/index'
import SecurityRouter from '@/components/view/security/index'
import Constants from '@/components/util/Constants';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      name: '首页',
      component: Login
    },
    {
      path: '/login',
      name: '登陆页',
      redirect: "/"
    },
    {
      path: '/home',
      name: 'home',
      component: ContentMain,
      redirect: '/index',
      children: [
        {
          path: '/index',
          name: '主页',
          component: Index
        },
        ...ProjectRouter,
        ...ConfigRouter,
        ...SecurityRouter,
      ]
    },
    {
      path: '*',
      redirect: '/'
    }
  ]
});

//Router路径拦截，除默认页都需Token认证
router.beforeEach((to, from, next) => {
  if (to.path === '/') { //登陆首页
    next();
  } else if (sessionStorage.getItem(Constants.TOKEN)) { //Token校验
    next();
  } else {  //默认登陆页
    next({
      path: Constants.LOGIN
    })
  }
});

export default router;